/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JinosJFrame.java
 *
 * Created on May 2, 2012, 5:44:28 AM
 */

package org.jinouts.ui;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.jinouts.ui.biz.JinoutsFrameController;

/**
 * 
 * @author asraf
 */
public class JinoutsJFrame extends javax.swing.JFrame
{
	private JinoutsFrameController controller;

	/** Creates new form JinosJFrame */
	public JinoutsJFrame( String title, JinoutsFrameController controller )
	{
		super ( title );
		this.controller = controller;
		initComponents ( );
		myInitComponents ( );
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	private void myInitComponents ( )
	{
		chooser = new JFileChooser ( );
		chooser.setFileSelectionMode ( JFileChooser.DIRECTORIES_ONLY );
		this.destTextField.setEditable ( false );
	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents ( )
	{

		wsdlLabel = new javax.swing.JLabel ( );
		wsdlTextField = new javax.swing.JTextField ( );
		destLabel = new javax.swing.JLabel ( );
		destTextField = new javax.swing.JTextField ( );
		destBrowseButton = new javax.swing.JButton ( );
		okButton = new javax.swing.JButton ( );
		cancelButton = new javax.swing.JButton ( );

		setDefaultCloseOperation ( javax.swing.WindowConstants.EXIT_ON_CLOSE );

		wsdlLabel.setFont ( new java.awt.Font ( "Tahoma", 1, 12 ) ); // NOI18N
		wsdlLabel.setText ( "WSDL File URL :" );

		wsdlTextField.addActionListener ( new java.awt.event.ActionListener ( )
		{
			public void actionPerformed ( java.awt.event.ActionEvent evt )
			{
				wsdlTextFieldActionPerformed ( evt );
			}
		} );

		destLabel.setFont ( new java.awt.Font ( "Tahoma", 1, 12 ) ); // NOI18N
		destLabel.setText ( "Choose Folder for Client Stub :" );

		destTextField.addActionListener ( new java.awt.event.ActionListener ( )
		{
			public void actionPerformed ( java.awt.event.ActionEvent evt )
			{
				destTextFieldActionPerformed ( evt );
			}
		} );

		destBrowseButton.setText ( "Browse" );
		destBrowseButton.addActionListener ( new java.awt.event.ActionListener ( )
		{
			public void actionPerformed ( java.awt.event.ActionEvent evt )
			{
				destBrowseButtonActionPerformed ( evt );
			}
		} );

		okButton.setText ( "Generate WS Client Stub" );
		okButton.addActionListener ( new java.awt.event.ActionListener ( )
		{
			public void actionPerformed ( java.awt.event.ActionEvent evt )
			{
				okButtonActionPerformed ( evt );
			}
		} );

		cancelButton.setText ( "Cancel" );
		cancelButton.addActionListener ( new java.awt.event.ActionListener ( )
		{
			public void actionPerformed ( java.awt.event.ActionEvent evt )
			{
				cancelButtoneActionPerformed ( evt );
			}
		} );

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout ( getContentPane ( ) );
		getContentPane ( ).setLayout ( layout );
		layout.setHorizontalGroup ( layout.createParallelGroup ( javax.swing.GroupLayout.Alignment.LEADING ).addGroup ( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup ( ).addContainerGap ( 68, Short.MAX_VALUE ).addGroup ( layout.createParallelGroup ( javax.swing.GroupLayout.Alignment.TRAILING ).addComponent ( wsdlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE ).addComponent ( destTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addGap ( 45, 45, 45 ).addComponent ( destBrowseButton ).addGap ( 50, 50, 50 ) ).addGroup ( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup ( ).addContainerGap ( 345, Short.MAX_VALUE ).addComponent ( okButton ).addGap ( 46, 46, 46 ).addComponent ( cancelButton ).addGap ( 121, 121, 121 ) ).addGroup ( layout.createSequentialGroup ( ).addGap ( 32, 32, 32 ).addGroup ( layout.createParallelGroup ( javax.swing.GroupLayout.Alignment.LEADING ).addComponent ( wsdlLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE ).addComponent ( destLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE ) ).addContainerGap ( 369, Short.MAX_VALUE ) ) );
		layout.setVerticalGroup ( layout.createParallelGroup ( javax.swing.GroupLayout.Alignment.LEADING ).addGroup ( layout.createSequentialGroup ( ).addGap ( 31, 31, 31 ).addComponent ( wsdlLabel ).addPreferredGap ( javax.swing.LayoutStyle.ComponentPlacement.UNRELATED ).addComponent ( wsdlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ).addGap ( 36, 36, 36 ).addComponent ( destLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE ).addGap ( 18, 18, 18 ).addGroup ( layout.createParallelGroup ( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent ( destTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ).addComponent ( destBrowseButton ) ).addPreferredGap ( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE ).addGroup ( layout.createParallelGroup ( javax.swing.GroupLayout.Alignment.BASELINE ).addComponent ( okButton ).addComponent ( cancelButton ) ).addGap ( 62, 62, 62 ) ) );

		pack ( );
	}// </editor-fold>//GEN-END:initComponents

	private void wsdlTextFieldActionPerformed ( java.awt.event.ActionEvent evt )
	{// GEN-FIRST:event_wsdlTextFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_wsdlTextFieldActionPerformed

	private void destTextFieldActionPerformed ( java.awt.event.ActionEvent evt )
	{// GEN-FIRST:event_destTextFieldActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_destTextFieldActionPerformed

	private void destBrowseButtonActionPerformed ( java.awt.event.ActionEvent evt )
	{
		int retval = chooser.showDialog ( this, null );
		if ( retval == JFileChooser.APPROVE_OPTION )
		{
			File theFile = chooser.getSelectedFile ( );
			if ( theFile != null )
			{
				if ( theFile.isDirectory ( ) )
				{
					this.destTextField.setText ( chooser.getSelectedFile ( ).getAbsolutePath ( ) );
				}
				else
				{

					JOptionPane.showMessageDialog ( this, "You chose a file, please choose a directory: " );
				}
				return;
			}
		}
		JOptionPane.showMessageDialog ( this, "Please Choose a directory" );
	}// GEN-LAST:event_destBrowseButtonActionPerformed

	private void okButtonActionPerformed ( java.awt.event.ActionEvent evt )
	{
		// check the wsdlurl
		String wsdlUrl = this.wsdlTextField.getText ( );
		
		if ( wsdlUrl==null || wsdlUrl.isEmpty ( ) )
		{
			JOptionPane.showMessageDialog ( this, "Please Give an entry of valid WSDL URL" );
			return;
		}
		
		File theFile = chooser.getSelectedFile ( );
		if ( theFile != null )
		{
			if ( !theFile.exists ( ) || !theFile.isDirectory ( ) )
			{
				JOptionPane.showMessageDialog ( this, "Please choose a valid directory" );
				return;
			}
			else
			{
				this.controller.generateJavaFromWSDL ( theFile, this.wsdlTextField.getText ( ) );
			}
		}
		else
		{
			JOptionPane.showMessageDialog ( this, "Please choose a directory" );
			return;
		}
	}

	private void cancelButtoneActionPerformed ( java.awt.event.ActionEvent evt )
	{// GEN-FIRST:event_cancelButtoneActionPerformed
		// TODO add your handling code here:
		System.exit ( 0 );
	}// GEN-LAST:event_cancelButtoneActionPerformed

	private JFileChooser chooser;

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton cancelButton;
	private javax.swing.JButton destBrowseButton;
	private javax.swing.JLabel destLabel;
	private javax.swing.JTextField destTextField;
	private javax.swing.JButton okButton;
	private javax.swing.JLabel wsdlLabel;
	private javax.swing.JTextField wsdlTextField;
	// End of variables declaration//GEN-END:variables

}
